<?php
class Managearticles_Data_Updateaction {
	
	private $database;
	private $secure;
	
	public function __construct(){
		$this->database = new Core_Driver_MysqlImproved();
		$this->secure = new Core_Data_Secure();
	}
	
	public function updateValues(array $postVars){
		
		$postVars = $this->secure->parseData($postVars);
		
		$oldPageName = $postVars['oldPageName'];
		$category = $postVars['category'];
		$pageName = $postVars['pageName'];
		$pageMetaTitle = $postVars['pageMetaTitle'];
		$pageKeywords = $postVars['pageKeywords'];
		$pageMetaDescription = $postVars['pageMetaDescription'];
		$pageTitle = $postVars['pageTitle'];
		$pageDescription = $postVars['pageDescription'];
		$pageContent = $postVars['pageContent'];
		$pageAccessLevel = $postVars['accessLevel'];
		$available = $postVars['available'];
		
		$this->addPageSettingsValues($oldPageName, $pageAccessLevel, $available, $pageName, $category);
		
		$this->addPageMetaValues($oldPageName, $pageName, $pageMetaTitle, $pageMetaDescription, $pageKeywords);
		
		$this->addPageContentValues($oldPageName, $pageName, $pageTitle, $pageDescription, $pageContent);
		
		echo '<meta http-equiv="Refresh" content="0;url='.SERVER_ROOT.'?managearticles">';
	}
	
	private function addPageSettingsValues($oldPAgeName, $pageAccessLevel, $available, $pageName, $category){
		$this->database->connect();
		$category = $this->database->escape($category);
		$pageAccessLevel = $this->database->escape($pageAccessLevel);
		$available = $this->database->escape($available);
		$this->database->prepare(
			"update page_settings SET page_name = '$pageName', page_category = '$category', page_access_level = '$pageAccessLevel', page_active = '$available' where page_name = '$oldPAgeName';"
		);
		$this->database->query();
		$this->database->dataOperations();
	}
	
	private function addPageMetaValues($oldPageName, $pageName, $pageMetaTitle, $pageMetaDescription, $pageKeywords){
		$this->database->connect();
		$oldPageName = $this->database->escape($oldPageName);
		$pageName = $this->database->escape($pageName);
		$pageMetaTitle = $this->database->escape($pageMetaTitle);
		$pageMetaDescription = $this->database->escape($pageMetaDescription);
		$pageKeywords = $this->database->escape($pageKeywords);
		$this->database->prepare(
			"update page_meta_values set page_name = '$pageName', page_meta_title = '$pageMetaTitle', page_meta_description = '$pageMetaDescription', page_meta_keywords = '$pageKeywords' where page_name = '$oldPageName';"
		);
		$this->database->query();
		$this->database->dataOperations();
	}
	
	private function addPageContentValues($oldPageName, $pageName, $pageTitle, $pageDescription, $pageContent){
		$this->database->connect();
		$oldPageName = $this->database->escape($oldPageName);
		$pageName = $this->database->escape($pageName);
		$pageTitle = $this->database->escape($pageTitle);
		$pageDescription = $this->database->escape($pageDescription);
		$pageContent = $this->database->escape($pageContent);
		$this->database->prepare(
			"
				update page_content_values SET page_name = '$pageName', page_title = '$pageTitle', page_description = '$pageDescription', page_content = '$pageContent'where page_name = '$oldPageName';
				"
		);
		$this->database->query();
		$this->database->dataOperations();
	}
}